<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>3.天气案例_watch_立即监视</title>
		<style>
			img{
				width: 100px;
			}
		</style>
		<!-- 引入Vue -->
		<script type="text/javascript" src="../js/vue.js"></script>
	</head>
	<body>
		<!-- 
			立即监视：
				1.配置：immediate:true
				2.作用：可以让数据没有发生变化时，Vue初次渲染时就调用一下监视的回调函数（handler）
		-->
		<!-- 准备好一个容器-->
		<div id="demo">
			<h2>今天天气很{{weatherText}}</h2>
			<img :src="url" alt=""> <br>
			<button @click="isHot = !isHot">切换天气</button>
		</div>

		<script type="text/javascript">
			//阻止 vue 在启动时生成生产提示。
			Vue.config.productionTip = false

			let vm = new Vue({
				el:'#demo',
				data:{
					isHot:true,
					url:'https://s3.ax1x.com/2020/12/13/reC1IA.jpg'
				},
				computed:{
					weatherText(){
						return this.isHot ? '炎热' : '凉爽'
					}
				},
				watch:{
					isHot:{
						immediate:true, //立即监视
						handler(newValue,oldValue){ 
							console.log('isHot属性监视的回调执行了',newValue,oldValue)
							if(newValue){
								this.url = 'https://s3.ax1x.com/2020/12/13/reC1IA.jpg'
							}else{
								this.url = 'https://s3.ax1x.com/2020/12/13/reCaqg.jpg'
							}
						}
					},
				}
			})

			//了解即可
			/* vm.$watch('isHot',{
				handler(newValue,oldValue){ 
					if(newValue){
						this.url = 'https://s3.ax1x.com/2020/12/13/reC1IA.jpg'
					}else{
						this.url = 'https://s3.ax1x.com/2020/12/13/reCaqg.jpg'
					}
				}
			}) */

			
		</script>
	</body>
</html>